#!/usr/bin/env python 
# -*- coding: utf-8 -*-

"""
# @Time    : 2021/1/15 18:13
# @Author  : Wang Yang
# @File    : EnterToken.py
"""

import wx
import re
import win32api
import win32ui
import win32con


class TextEntryDialog(wx.Dialog):

    def __init__(self, parent=None, title='Token input box', caption='Token', size=(50, 25)):
        style = wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER
        local = wx.Locale(wx.LANGUAGE_ENGLISH)
        if local.IsOk():
            local.AddCatalogLookupPathPrefix('local')
            rst = local.AddCatalog('autoupdate')
            # print(rst)
        super(TextEntryDialog, self).__init__(parent, -1, title=title, style=style)
        self.text = wx.StaticText(self, -1, caption)
        self.input = wx.TextCtrl(self, -1, style=wx.TE_LEFT)
        self.input.SetInitialSize(size)
        self.buttons = self.CreateButtonSizer(wx.OK | wx.CANCEL)
        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.sizer.Add(self.text, 0, wx.ALL, 5)
        self.sizer.Add(self.input, 1, wx.EXPAND | wx.ALL, 5)
        self.sizer.Add(self.buttons, 0, wx.EXPAND | wx.ALL, 5)
        self.SetSizerAndFit(self.sizer)
        self.Center()

    def set_value(self, value):
        self.input.SetValue(value)

    def get_value(self):
        return self.input.GetValue()


def input_token_use_dialog():
    app = wx.App()
    dialog = TextEntryDialog()
    dialog.set_value('888888')
    dialog.input.SelectAll()
    dialog.input.SetFocus()
    if dialog.ShowModal() == wx.ID_OK:
        token = dialog.get_value()
        # print(type(token))
        print("Token is {}".format(token))
        dialog.Destroy()
        app.MainLoop()
        if not re.match(r'\d{6}$', token):
            raise Exception("Token format is not correct")
    else:
        dialog.Destroy()
        app.MainLoop()
        raise Exception("Not confirm")
    return token


if __name__ == "__main__":
    input_token_use_dialog()
